package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.vo.NAsphaltDataWeightPageVo;
import com.ruoyi.zhgd.domain.vo.NAsphaltProduceWeightVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltDataWeightBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltDataWeightVo;
import com.ruoyi.zhgd.domain.NAsphaltDataWeight;
import com.ruoyi.zhgd.mapper.NAsphaltDataWeightMapper;
import com.ruoyi.zhgd.service.INAsphaltDataWeightService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 沥青拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltDataWeightServiceImpl implements INAsphaltDataWeightService {

    private final NAsphaltDataWeightMapper baseMapper;

    /**
     * 查询沥青拌合站数据
     */
    @Override
    public Map<String, NAsphaltDataWeightVo> queryById(String id){
        NAsphaltDataWeightVo vo = baseMapper.getById(id);
        Map<String, NAsphaltDataWeightVo> data = new LinkedHashMap<>();
        if (ObjectUtils.isNotEmpty(vo)) {
            NAsphaltDataWeightVo entity1 = new NAsphaltDataWeightVo(); // 实际
            NAsphaltDataWeightVo entity2 = new NAsphaltDataWeightVo(); // 理论
            NAsphaltDataWeightVo entity3 = new NAsphaltDataWeightVo(); // 差值
            // 实际
            entity1.setId(vo.getId());
            entity1.setDeptName(vo.getDeptName());
            entity1.setDischargeTime(vo.getDischargeTime());
            entity1.setMatchCode(vo.getMatchCode());
            entity1.setMixingDuration(vo.getMixingDuration());
            entity1.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity1.setDesc("实际值");
            entity1.setBatchWeight(vo.getBatchWeight());
            entity1.setGravel1Act(vo.getGravel1Act());
            entity1.setGravel2Act(vo.getGravel2Act());
            entity1.setGravel3Act(vo.getGravel3Act());
            entity1.setGravel4Act(vo.getGravel4Act());
            entity1.setGravel5Act(vo.getGravel5Act());
            entity1.setGravel6Act(vo.getGravel6Act());
            entity1.setPowder1Act(vo.getPowder1Act());
            entity1.setPowder2Act(vo.getPowder2Act());
            entity1.setPowder3Act(vo.getPowder3Act());
            entity1.setAsphaltAct(vo.getAsphaltAct());
            entity1.setRecycledMaterialAct(vo.getRecycledMaterialAct());
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
//            BigDecimal sum1 = vo.getGravel1Act().add(vo.getGravel2Act()).add(vo.getGravel3Act()).add(vo.getGravel4Act()).add(vo.getGravel5Act()).add(vo.getGravel6Act());
//            BigDecimal a1 = null;
//            if (ObjectUtils.isNotEmpty(vo.getAsphaltAct()) && !(sum1.compareTo(BigDecimal.ZERO) == 0)) {
//                a1 = vo.getAsphaltAct().divide(sum1,2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//                entity1.setAsphaltAggregateRatio(a1);
//            }
            entity1.setAsphaltAggregateRatio(getSjYsb(vo));

            // 理论
            entity2.setId(vo.getId());
            entity2.setDeptName(vo.getDeptName());
            entity2.setDischargeTime(vo.getDischargeTime());
            entity3.setMatchCode(vo.getMatchCode());
            entity2.setMixingDuration(vo.getMixingDuration());
            entity2.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity2.setDesc("理论值");
            entity2.setBatchWeight(null);
            entity2.setGravel1Act(vo.getGravel1Theo());
            entity2.setGravel2Act(vo.getGravel2Theo());
            entity2.setGravel3Act(vo.getGravel3Theo());
            entity2.setGravel4Act(vo.getGravel4Theo());
            entity2.setGravel5Act(vo.getGravel5Theo());
            entity2.setGravel6Act(vo.getGravel6Theo());
            entity2.setPowder1Act(vo.getPowder1Theo());
            entity2.setPowder2Act(vo.getPowder2Theo());
            entity2.setPowder3Act(vo.getPowder3Theo());
            entity2.setAsphaltAct(vo.getAsphaltTheo());
            entity2.setRecycledMaterialAct(vo.getRecycledMaterialTheo());
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
//            BigDecimal sum2 = vo.getGravel1Theo().add(vo.getGravel2Theo()).add(vo.getGravel3Theo()).add(vo.getGravel4Theo()).add(vo.getGravel5Theo()).add(vo.getGravel6Theo());
//            BigDecimal  a2 = null;
//            if (ObjectUtils.isNotEmpty(vo.getAsphaltTheo()) && !(sum2.compareTo(BigDecimal.ZERO) == 0)) {
//                a2 = vo.getAsphaltTheo().divide(sum2,2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//                entity2.setAsphaltAggregateRatio(a2);
//            }
            entity2.setAsphaltAggregateRatio(getLlYsb(vo));

            // 差值
            entity3.setId(vo.getId());
            entity3.setDeptName(vo.getDeptName());
            entity3.setDischargeTime(vo.getDischargeTime());
            entity3.setMatchCode(vo.getMatchCode());
            entity3.setMixingDuration(vo.getMixingDuration());
            entity3.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity3.setDesc("差值");
            entity3.setBatchWeight(null);
            entity3.setGravel1Act(ObjectUtils.isNotEmpty(vo.getGravel1Act()) && ObjectUtils.isNotEmpty(vo.getGravel1Theo()) ? vo.getGravel1Act().subtract(vo.getGravel1Theo()) : null);
            entity3.setGravel2Act(ObjectUtils.isNotEmpty(vo.getGravel2Act()) && ObjectUtils.isNotEmpty(vo.getGravel2Theo()) ? vo.getGravel2Act().subtract(vo.getGravel2Theo()) : null);
            entity3.setGravel3Act(ObjectUtils.isNotEmpty(vo.getGravel3Act()) && ObjectUtils.isNotEmpty(vo.getGravel3Theo()) ? vo.getGravel3Act().subtract(vo.getGravel3Theo()) : null);
            entity3.setGravel4Act(ObjectUtils.isNotEmpty(vo.getGravel4Act()) && ObjectUtils.isNotEmpty(vo.getGravel4Theo()) ? vo.getGravel4Act().subtract(vo.getGravel4Theo()) : null);
            entity3.setGravel5Act(ObjectUtils.isNotEmpty(vo.getGravel5Act()) && ObjectUtils.isNotEmpty(vo.getGravel5Theo()) ? vo.getGravel5Act().subtract(vo.getGravel5Theo()) : null);
            entity3.setGravel6Act(ObjectUtils.isNotEmpty(vo.getGravel6Act()) && ObjectUtils.isNotEmpty(vo.getGravel6Theo()) ? vo.getGravel6Act().subtract(vo.getGravel6Theo()) : null);
            entity3.setPowder1Act(ObjectUtils.isNotEmpty(vo.getPowder1Act()) && ObjectUtils.isNotEmpty(vo.getPowder1Theo()) ? vo.getPowder1Act().subtract(vo.getPowder1Theo()) : null);
            entity3.setPowder2Act(ObjectUtils.isNotEmpty(vo.getPowder2Act()) && ObjectUtils.isNotEmpty(vo.getPowder2Theo()) ? vo.getPowder2Act().subtract(vo.getPowder2Theo()) : null);
            entity3.setPowder3Act(ObjectUtils.isNotEmpty(vo.getPowder3Act()) && ObjectUtils.isNotEmpty(vo.getPowder3Theo()) ? vo.getPowder3Act().subtract(vo.getPowder3Theo()) : null);
            entity3.setAsphaltAct(ObjectUtils.isNotEmpty(vo.getAsphaltAct()) && ObjectUtils.isNotEmpty(vo.getAsphaltTheo()) ? vo.getAsphaltAct().subtract(vo.getAsphaltTheo()) : null);
            entity3.setRecycledMaterialAct(
                ObjectUtils.isNotEmpty(vo.getRecycledMaterialAct()) && ObjectUtils.isNotEmpty(vo.getRecycledMaterialTheo()) ?
                    vo.getRecycledMaterialAct().subtract(vo.getRecycledMaterialTheo()) :
                    null);
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
            BigDecimal sum3 = ObjectUtils.isNotEmpty(getSjYsb(vo)) && ObjectUtils.isNotEmpty(getLlYsb(vo)) ? getSjYsb(vo).subtract(getLlYsb(vo)) : null;
            entity3.setAsphaltAggregateRatio(sum3);

            data.put("sjz", entity1);
            data.put("llz", entity2);
            data.put("cz", entity3);
        } else {
            data.put("sjz", null);
            data.put("llz", null);
            data.put("cz", null);
        }
        return data;
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public TableDataInfo<NAsphaltDataWeightVo> queryPageList(NAsphaltDataWeightBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltDataWeight> lqw = buildQueryWrapper(bo);
        Page<NAsphaltDataWeightVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public List<NAsphaltDataWeightVo> queryList(NAsphaltDataWeightBo bo) {
        LambdaQueryWrapper<NAsphaltDataWeight> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltDataWeight> buildQueryWrapper(NAsphaltDataWeightBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltDataWeight> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAsphaltAct() != null, NAsphaltDataWeight::getAsphaltAct, bo.getAsphaltAct());
        lqw.eq(bo.getGravel1Act() != null, NAsphaltDataWeight::getGravel1Act, bo.getGravel1Act());
        lqw.eq(bo.getGravel2Act() != null, NAsphaltDataWeight::getGravel2Act, bo.getGravel2Act());
        lqw.eq(bo.getGravel3Act() != null, NAsphaltDataWeight::getGravel3Act, bo.getGravel3Act());
        lqw.eq(bo.getGravel4Act() != null, NAsphaltDataWeight::getGravel4Act, bo.getGravel4Act());
        lqw.eq(bo.getGravel5Act() != null, NAsphaltDataWeight::getGravel5Act, bo.getGravel5Act());
        lqw.eq(bo.getGravel6Act() != null, NAsphaltDataWeight::getGravel6Act, bo.getGravel6Act());
        lqw.eq(bo.getPowder1Act() != null, NAsphaltDataWeight::getPowder1Act, bo.getPowder1Act());
        lqw.eq(bo.getPowder2Act() != null, NAsphaltDataWeight::getPowder2Act, bo.getPowder2Act());
        lqw.eq(bo.getPowder3Act() != null, NAsphaltDataWeight::getPowder3Act, bo.getPowder3Act());
        lqw.eq(bo.getRecycledMaterialAct() != null, NAsphaltDataWeight::getRecycledMaterialAct, bo.getRecycledMaterialAct());
        lqw.eq(bo.getGravelAct() != null, NAsphaltDataWeight::getGravelAct, bo.getGravelAct());
        lqw.eq(bo.getAsphaltTheo() != null, NAsphaltDataWeight::getAsphaltTheo, bo.getAsphaltTheo());
        lqw.eq(bo.getPowderAct() != null, NAsphaltDataWeight::getPowderAct, bo.getPowderAct());
        lqw.eq(bo.getGravel1Theo() != null, NAsphaltDataWeight::getGravel1Theo, bo.getGravel1Theo());
        lqw.eq(bo.getGravel2Theo() != null, NAsphaltDataWeight::getGravel2Theo, bo.getGravel2Theo());
        lqw.eq(bo.getGravel3Theo() != null, NAsphaltDataWeight::getGravel3Theo, bo.getGravel3Theo());
        lqw.eq(bo.getGravel4Theo() != null, NAsphaltDataWeight::getGravel4Theo, bo.getGravel4Theo());
        lqw.eq(bo.getGravel5Theo() != null, NAsphaltDataWeight::getGravel5Theo, bo.getGravel5Theo());
        lqw.eq(bo.getGravel6Theo() != null, NAsphaltDataWeight::getGravel6Theo, bo.getGravel6Theo());
        lqw.eq(bo.getPowder1Theo() != null, NAsphaltDataWeight::getPowder1Theo, bo.getPowder1Theo());
        lqw.eq(bo.getPowder2Theo() != null, NAsphaltDataWeight::getPowder2Theo, bo.getPowder2Theo());
        lqw.eq(bo.getPowder3Theo() != null, NAsphaltDataWeight::getPowder3Theo, bo.getPowder3Theo());
        lqw.eq(bo.getRecycledMaterialTheo() != null, NAsphaltDataWeight::getRecycledMaterialTheo, bo.getRecycledMaterialTheo());
        lqw.eq(bo.getPowderTheo() != null, NAsphaltDataWeight::getPowderTheo, bo.getPowderTheo());
        lqw.eq(bo.getGravelTheo() != null, NAsphaltDataWeight::getGravelTheo, bo.getGravelTheo());
        lqw.eq(bo.getAsphaltCold() != null, NAsphaltDataWeight::getAsphaltCold, bo.getAsphaltCold());
        lqw.eq(bo.getGravel1Cold() != null, NAsphaltDataWeight::getGravel1Cold, bo.getGravel1Cold());
        lqw.eq(bo.getGravel2Cold() != null, NAsphaltDataWeight::getGravel2Cold, bo.getGravel2Cold());
        lqw.eq(bo.getGravel3Cold() != null, NAsphaltDataWeight::getGravel3Cold, bo.getGravel3Cold());
        lqw.eq(bo.getGravel4Cold() != null, NAsphaltDataWeight::getGravel4Cold, bo.getGravel4Cold());
        lqw.eq(bo.getGravel5Cold() != null, NAsphaltDataWeight::getGravel5Cold, bo.getGravel5Cold());
        lqw.eq(bo.getGravel6Cold() != null, NAsphaltDataWeight::getGravel6Cold, bo.getGravel6Cold());
        lqw.eq(bo.getPowder1Cold() != null, NAsphaltDataWeight::getPowder1Cold, bo.getPowder1Cold());
        lqw.eq(bo.getPowder2Cold() != null, NAsphaltDataWeight::getPowder2Cold, bo.getPowder2Cold());
        lqw.eq(bo.getPowder3Cold() != null, NAsphaltDataWeight::getPowder3Cold, bo.getPowder3Cold());
        lqw.eq(bo.getRecycledMaterialCold() != null, NAsphaltDataWeight::getRecycledMaterialCold, bo.getRecycledMaterialCold());
        lqw.eq(bo.getWoodFiberCold() != null, NAsphaltDataWeight::getWoodFiberCold, bo.getWoodFiberCold());
        lqw.eq(bo.getGravelCold() != null, NAsphaltDataWeight::getGravelCold, bo.getGravelCold());
        lqw.eq(bo.getPowderCold() != null, NAsphaltDataWeight::getPowderCold, bo.getPowderCold());
        return lqw;
    }

    /**
     * 新增沥青拌合站数据
     */
    @Override
    public Boolean insertByBo(NAsphaltDataWeightBo bo) {
        NAsphaltDataWeight add = BeanUtil.toBean(bo, NAsphaltDataWeight.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站数据
     */
    @Override
    public Boolean updateByBo(NAsphaltDataWeightBo bo) {
        NAsphaltDataWeight update = BeanUtil.toBean(bo, NAsphaltDataWeight.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltDataWeight entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生产数据监控重量分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltDataWeightPageVo> page(NAsphaltDataWeightBo bo, PageQuery pageQuery) {
        Page<NAsphaltDataWeightPageVo> res = new Page<>();
        Page<NAsphaltDataWeightVo> result = baseMapper.page(pageQuery.build(), bo);
        if (ObjectUtils.isNotEmpty(result.getRecords()) && result.getRecords().size() > 0) {
            List<NAsphaltDataWeightVo> data = handleList(result.getRecords(), true, true);
            result.setRecords(data);
            // 添加差值百分比处理
            Map<String, List<NAsphaltDataWeightVo>> map = data.stream().collect(Collectors.groupingBy(NAsphaltDataWeightVo::getId));
            List<NAsphaltDataWeightPageVo> itemList = new ArrayList<>();
            map.forEach((key, item) -> {
                if (CollectionUtil.isNotEmpty(item)) {
                    NAsphaltDataWeightPageVo cz = null;
                    NAsphaltDataWeightPageVo per = null;
                    for (NAsphaltDataWeightVo weightVo : item) {
                        if (ObjectUtils.isNotEmpty(weightVo) && ObjectUtils.isNotEmpty(weightVo.getDesc()) && "实际值".equals(weightVo.getDesc())) {
                            NAsphaltDataWeightPageVo copy = BeanUtil.copyProperties(weightVo, NAsphaltDataWeightPageVo.class);
                            itemList.add(copy);
                        }
                        if (ObjectUtils.isNotEmpty(weightVo) && ObjectUtils.isNotEmpty(weightVo.getDesc()) && "理论值".equals(weightVo.getDesc())) {
                            NAsphaltDataWeightPageVo copy = BeanUtil.copyProperties(weightVo, NAsphaltDataWeightPageVo.class);
                            itemList.add(copy);
                        }
                        if (ObjectUtils.isNotEmpty(weightVo) && ObjectUtils.isNotEmpty(weightVo.getDesc()) && "差值".equals(weightVo.getDesc())) {
                            cz = BeanUtil.copyProperties(weightVo, NAsphaltDataWeightPageVo.class);
                        }
                        if (ObjectUtils.isNotEmpty(weightVo) && ObjectUtils.isNotEmpty(weightVo.getDesc()) && "差值百分比".equals(weightVo.getDesc())) {
                            per = BeanUtil.copyProperties(weightVo, NAsphaltDataWeightPageVo.class);
                        }
                    }
                    if (ObjectUtils.isNotEmpty(cz) && ObjectUtils.isNotEmpty(per)) {
                        if (ObjectUtils.isNotEmpty(cz.getGravel1Act()) && ObjectUtils.isNotEmpty(per.getGravel1Act())) {
                            cz.setGravel1Act(cz.getGravel1Act() + "("+ per.getGravel1Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getGravel2Act()) && ObjectUtils.isNotEmpty(per.getGravel2Act())) {
                            cz.setGravel2Act(cz.getGravel2Act() + "("+ per.getGravel2Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getGravel3Act()) && ObjectUtils.isNotEmpty(per.getGravel3Act())) {
                            cz.setGravel3Act(cz.getGravel3Act() + "("+ per.getGravel3Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getGravel4Act()) && ObjectUtils.isNotEmpty(per.getGravel4Act())) {
                            cz.setGravel4Act(cz.getGravel4Act() + "("+ per.getGravel4Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getGravel5Act()) && ObjectUtils.isNotEmpty(per.getGravel5Act())) {
                            cz.setGravel5Act(cz.getGravel5Act() + "("+ per.getGravel5Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getGravel6Act()) && ObjectUtils.isNotEmpty(per.getGravel6Act())) {
                            cz.setGravel6Act(cz.getGravel6Act() + "("+ per.getGravel6Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getPowder1Act()) && ObjectUtils.isNotEmpty(per.getPowder1Act())) {
                            cz.setPowder1Act(cz.getPowder1Act() + "("+ per.getPowder1Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getPowder2Act()) && ObjectUtils.isNotEmpty(per.getPowder2Act())) {
                            cz.setPowder2Act(cz.getPowder2Act() + "("+ per.getPowder2Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getPowder3Act()) && ObjectUtils.isNotEmpty(per.getPowder3Act())) {
                            cz.setPowder3Act(cz.getPowder3Act() + "("+ per.getPowder3Act() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getRecycledMaterialAct()) && ObjectUtils.isNotEmpty(per.getRecycledMaterialAct())) {
                            cz.setRecycledMaterialAct(cz.getRecycledMaterialAct() + "("+ per.getRecycledMaterialAct() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getAsphaltAct()) && ObjectUtils.isNotEmpty(per.getAsphaltAct())) {
                            cz.setAsphaltAct(cz.getAsphaltAct() + "("+ per.getAsphaltAct() +"%)");
                        }
                        if (ObjectUtils.isNotEmpty(cz.getAsphaltAggregateRatio()) && ObjectUtils.isNotEmpty(per.getAsphaltAggregateRatio())) {
                            cz.setAsphaltAggregateRatio(cz.getAsphaltAggregateRatio() + "("+ per.getAsphaltAggregateRatio() +"%)");
                        }

                        itemList.add(cz);
                    }

                }

            });

            res.setTotal(result.getTotal());
            res.setRecords(itemList);
        }
        return TableDataInfo.build(res);
    }

    /**
     * 重量数据查询处理
     * @param list
     * @return
     */
    @Override
    public List<NAsphaltDataWeightVo> handleList(List<NAsphaltDataWeightVo> list, boolean isQueryAll, boolean czPer) {
        List<NAsphaltDataWeightVo> data = new ArrayList<>();
        for (NAsphaltDataWeightVo vo : list) {
            NAsphaltDataWeightVo entity1 = new NAsphaltDataWeightVo(); // 实际
            NAsphaltDataWeightVo entity2 = new NAsphaltDataWeightVo(); // 理论
            NAsphaltDataWeightVo entity3 = new NAsphaltDataWeightVo(); // 差值
            NAsphaltDataWeightVo entity4 = new NAsphaltDataWeightVo(); // 差值百分比
            // 实际
            entity1.setId(vo.getId());
            entity1.setDeptId(vo.getDeptId());
            entity1.setMatchName(vo.getMatchName());
            entity1.setDeptName(vo.getDeptName());
            entity1.setDischargeTime(vo.getDischargeTime());
            entity1.setMatchCode(vo.getMatchCode());
            entity1.setMixingDuration(vo.getMixingDuration());
            entity1.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity1.setDesc("实际值");
            entity1.setBatchWeight(vo.getBatchWeight());
            entity1.setGravel1Act(vo.getGravel1Act());
            entity1.setGravel2Act(vo.getGravel2Act());
            entity1.setGravel3Act(vo.getGravel3Act());
            entity1.setGravel4Act(vo.getGravel4Act());
            entity1.setGravel5Act(vo.getGravel5Act());
            entity1.setGravel6Act(vo.getGravel6Act());
            entity1.setPowder1Act(vo.getPowder1Act());
            entity1.setPowder2Act(vo.getPowder2Act());
            entity1.setPowder3Act(vo.getPowder3Act());
            entity1.setAsphaltAct(vo.getAsphaltAct());
            entity1.setRecycledMaterialAct(vo.getRecycledMaterialAct());
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
//                BigDecimal sum1 = vo.getGravel1Act().add(vo.getGravel2Act()).add(vo.getGravel3Act()).add(vo.getGravel4Act()).add(vo.getGravel5Act()).add(vo.getGravel6Act());
//                BigDecimal a1 = null;
//                if (ObjectUtils.isNotEmpty(vo.getAsphaltAct()) && !(sum1.compareTo(BigDecimal.ZERO) == 0)) {
//                    a1 = vo.getAsphaltAct().divide(sum1,2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//                    entity1.setAsphaltAggregateRatio(a1);
//                }
            entity1.setAsphaltAggregateRatio(getSjYsb(vo));
            entity1.setTotalWeight(getZl(vo));

            // 理论
            entity2.setId(vo.getId());
            entity2.setDeptId(vo.getDeptId());
            entity2.setMatchName(vo.getMatchName());
            entity2.setDeptName(vo.getDeptName());
            entity2.setDischargeTime(vo.getDischargeTime());
            entity3.setMatchCode(vo.getMatchCode());
            entity2.setMixingDuration(vo.getMixingDuration());
            entity2.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity2.setDesc("理论值");
            entity2.setBatchWeight(null);
            entity2.setGravel1Act(vo.getGravel1Theo());
            entity2.setGravel2Act(vo.getGravel2Theo());
            entity2.setGravel3Act(vo.getGravel3Theo());
            entity2.setGravel4Act(vo.getGravel4Theo());
            entity2.setGravel5Act(vo.getGravel5Theo());
            entity2.setGravel6Act(vo.getGravel6Theo());
            entity2.setPowder1Act(vo.getPowder1Theo());
            entity2.setPowder2Act(vo.getPowder2Theo());
            entity2.setPowder3Act(vo.getPowder3Theo());
            entity2.setAsphaltAct(vo.getAsphaltTheo());
            entity2.setRecycledMaterialAct(vo.getRecycledMaterialTheo());
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
//                BigDecimal sum2 = vo.getGravel1Theo().add(vo.getGravel2Theo()).add(vo.getGravel3Theo()).add(vo.getGravel4Theo()).add(vo.getGravel5Theo()).add(vo.getGravel6Theo());
//                BigDecimal  a2 = null;
//                if (ObjectUtils.isNotEmpty(vo.getAsphaltTheo()) && !(sum2.compareTo(BigDecimal.ZERO) == 0)) {
//                    a2 = vo.getAsphaltTheo().divide(sum2,2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//                    entity2.setAsphaltAggregateRatio(a2);
//                }
            entity2.setAsphaltAggregateRatio(getLlYsb(vo));
            entity2.setTotalWeight(getZl(vo));

            // 差值
            entity3.setId(vo.getId());
            entity3.setDeptId(vo.getDeptId());
            entity3.setMatchName(vo.getMatchName());
            entity3.setDeptName(vo.getDeptName());
            entity3.setDischargeTime(vo.getDischargeTime());
            entity3.setMatchCode(vo.getMatchCode());
            entity3.setMixingDuration(vo.getMixingDuration());
            entity3.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity3.setDesc("差值");
            entity3.setBatchWeight(null);
            entity3.setGravel1Act(ObjectUtils.isNotEmpty(vo.getGravel1Act()) && ObjectUtils.isNotEmpty(vo.getGravel1Theo()) ? vo.getGravel1Act().subtract(vo.getGravel1Theo()) : null);
            entity3.setGravel2Act(ObjectUtils.isNotEmpty(vo.getGravel2Act()) && ObjectUtils.isNotEmpty(vo.getGravel2Theo()) ? vo.getGravel2Act().subtract(vo.getGravel2Theo()) : null);
            entity3.setGravel3Act(ObjectUtils.isNotEmpty(vo.getGravel3Act()) && ObjectUtils.isNotEmpty(vo.getGravel3Theo()) ? vo.getGravel3Act().subtract(vo.getGravel3Theo()) : null);
            entity3.setGravel4Act(ObjectUtils.isNotEmpty(vo.getGravel4Act()) && ObjectUtils.isNotEmpty(vo.getGravel4Theo()) ? vo.getGravel4Act().subtract(vo.getGravel4Theo()) : null);
            entity3.setGravel5Act(ObjectUtils.isNotEmpty(vo.getGravel5Act()) && ObjectUtils.isNotEmpty(vo.getGravel5Theo()) ? vo.getGravel5Act().subtract(vo.getGravel5Theo()) : null);
            entity3.setGravel6Act(ObjectUtils.isNotEmpty(vo.getGravel6Act()) && ObjectUtils.isNotEmpty(vo.getGravel6Theo()) ? vo.getGravel6Act().subtract(vo.getGravel6Theo()) : null);
            entity3.setPowder1Act(ObjectUtils.isNotEmpty(vo.getPowder1Act()) && ObjectUtils.isNotEmpty(vo.getPowder1Theo()) ? vo.getPowder1Act().subtract(vo.getPowder1Theo()) : null);
            entity3.setPowder2Act(ObjectUtils.isNotEmpty(vo.getPowder2Act()) && ObjectUtils.isNotEmpty(vo.getPowder2Theo()) ? vo.getPowder2Act().subtract(vo.getPowder2Theo()) : null);
            entity3.setPowder3Act(ObjectUtils.isNotEmpty(vo.getPowder3Act()) && ObjectUtils.isNotEmpty(vo.getPowder3Theo()) ? vo.getPowder3Act().subtract(vo.getPowder3Theo()) : null);
            entity3.setAsphaltAct(ObjectUtils.isNotEmpty(vo.getAsphaltAct()) && ObjectUtils.isNotEmpty(vo.getAsphaltTheo()) ? vo.getAsphaltAct().subtract(vo.getAsphaltTheo()) : null);
            entity3.setRecycledMaterialAct(
                ObjectUtils.isNotEmpty(vo.getRecycledMaterialAct()) && ObjectUtils.isNotEmpty(vo.getRecycledMaterialTheo()) ?
                    vo.getRecycledMaterialAct().subtract(vo.getRecycledMaterialTheo()) :
                    null);
            // 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
            BigDecimal sum3 = ObjectUtils.isNotEmpty(getSjYsb(vo)) && ObjectUtils.isNotEmpty(getLlYsb(vo)) ? getSjYsb(vo).subtract(getLlYsb(vo)) : null;
            entity3.setAsphaltAggregateRatio(sum3);
            entity3.setTotalWeight(getZl(vo));

            // 差值百分比
            entity4.setId(vo.getId());
            entity4.setDeptId(vo.getDeptId());
            entity4.setMatchName(vo.getMatchName());
            entity4.setDeptName(vo.getDeptName());
            entity4.setDischargeTime(vo.getDischargeTime());
            entity4.setMatchCode(vo.getMatchCode());
            entity4.setMixingDuration(vo.getMixingDuration());
            entity4.setAsphaltTemperature(vo.getAsphaltTemperature());
            entity4.setDesc("差值百分比");
            entity4.setBatchWeight(null);
            entity4.setGravel1Act(
                ObjectUtils.isNotEmpty(entity3.getGravel1Act()) && ObjectUtils.isNotEmpty(entity2.getGravel1Act()) && entity2.getGravel1Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel1Act().divide(entity2.getGravel1Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setGravel2Act(
                ObjectUtils.isNotEmpty(entity3.getGravel2Act()) && ObjectUtils.isNotEmpty(entity2.getGravel2Act()) && entity2.getGravel2Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel2Act().divide(entity2.getGravel2Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setGravel3Act(
                ObjectUtils.isNotEmpty(entity3.getGravel3Act()) && ObjectUtils.isNotEmpty(entity2.getGravel3Act()) && entity2.getGravel3Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel3Act().divide(entity2.getGravel3Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setGravel4Act(
                ObjectUtils.isNotEmpty(entity3.getGravel4Act()) && ObjectUtils.isNotEmpty(entity2.getGravel4Act()) && entity2.getGravel4Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel4Act().divide(entity2.getGravel4Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setGravel5Act(
                ObjectUtils.isNotEmpty(entity3.getGravel5Act()) && ObjectUtils.isNotEmpty(entity2.getGravel5Act()) && entity2.getGravel5Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel5Act().divide(entity2.getGravel5Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setGravel6Act(
                ObjectUtils.isNotEmpty(entity3.getGravel6Act()) && ObjectUtils.isNotEmpty(entity2.getGravel6Act()) && entity2.getGravel6Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getGravel6Act().divide(entity2.getGravel6Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setPowder1Act(
                ObjectUtils.isNotEmpty(entity3.getPowder1Act()) && ObjectUtils.isNotEmpty(entity2.getPowder1Act()) && entity2.getPowder1Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getPowder1Act().divide(entity2.getPowder1Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setPowder2Act(
                ObjectUtils.isNotEmpty(entity3.getPowder2Act()) && ObjectUtils.isNotEmpty(entity2.getPowder2Act()) && entity2.getPowder2Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getPowder2Act().divide(entity2.getPowder2Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setPowder3Act(
                ObjectUtils.isNotEmpty(entity3.getPowder3Act()) && ObjectUtils.isNotEmpty(entity2.getPowder3Act()) && entity2.getPowder3Act().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getPowder3Act().divide(entity2.getPowder3Act(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setAsphaltAct(
                ObjectUtils.isNotEmpty(entity3.getAsphaltAct()) && ObjectUtils.isNotEmpty(entity2.getAsphaltAct()) && entity2.getAsphaltAct().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getAsphaltAct().divide(entity2.getAsphaltAct(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setRecycledMaterialAct(
                ObjectUtils.isNotEmpty(entity3.getRecycledMaterialAct()) && ObjectUtils.isNotEmpty(entity2.getRecycledMaterialAct()) && entity2.getRecycledMaterialAct().compareTo(BigDecimal.ZERO) != 0
                    ? entity3.getRecycledMaterialAct().divide(entity2.getRecycledMaterialAct(),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)) : null);
            entity4.setAsphaltAggregateRatio(entity3.getAsphaltAggregateRatio());

            data.add(entity1);
            if (isQueryAll) {
                data.add(entity2);
                data.add(entity3);
            }
            if (czPer) {
                data.add(entity4);
            }
        }
        return data;
    }

    /**
     * 实际 油石比计算:
     * 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
     * @param vo
     * @return
     */
    public BigDecimal getSjYsb(NAsphaltDataWeightVo vo) {
        BigDecimal res = BigDecimal.ZERO;
        BigDecimal sum = BigDecimal.ZERO;
        if (ObjectUtils.isNotEmpty(vo.getGravel1Act())) {
            sum = sum.add(vo.getGravel1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel2Act())) {
            sum = sum.add(vo.getGravel2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel3Act())) {
            sum = sum.add(vo.getGravel3Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel4Act())) {
            sum = sum.add(vo.getGravel4Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel5Act())) {
            sum = sum.add(vo.getGravel5Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel6Act())) {
            sum = sum.add(vo.getGravel6Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder1Act())) {
            sum = sum.add(vo.getPowder1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder2Act())) {
            sum = sum.add(vo.getPowder2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder3Act())) {
            sum = sum.add(vo.getPowder3Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getAsphaltAct()) && !(sum.compareTo(BigDecimal.ZERO) == 0)) {
            res = vo.getAsphaltAct().divide(sum,4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        }
        return res;
    }
    /**
     * 理论 油石比计算:
     * 油石比 = 沥青 / (骨料1+...+骨料6+粉料1+..+粉料3)
     * @param vo
     * @return
     */
    public BigDecimal getLlYsb(NAsphaltDataWeightVo vo) {
        BigDecimal res = BigDecimal.ZERO;
        BigDecimal sum = BigDecimal.ZERO;
        if (ObjectUtils.isNotEmpty(vo.getGravel1Theo())) {
            sum = sum.add(vo.getGravel1Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel2Theo())) {
            sum = sum.add(vo.getGravel2Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel3Theo())) {
            sum = sum.add(vo.getGravel3Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel4Theo())) {
            sum = sum.add(vo.getGravel4Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel5Theo())) {
            sum = sum.add(vo.getGravel5Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel6Theo())) {
            sum = sum.add(vo.getGravel6Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder1Theo())) {
            sum = sum.add(vo.getPowder1Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder2Theo())) {
            sum = sum.add(vo.getPowder2Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder3Theo())) {
            sum = sum.add(vo.getPowder3Theo());
        }
        if (ObjectUtils.isNotEmpty(vo.getAsphaltTheo()) && !(sum.compareTo(BigDecimal.ZERO) == 0)) {
            res = vo.getAsphaltTheo().divide(sum,4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        }
        return res;
    }

    /**
     * 获取生产总量
     * @param vo
     * @return
     */
    public BigDecimal getZl(NAsphaltDataWeightVo vo) {
        BigDecimal sum = BigDecimal.ZERO;
        if (ObjectUtils.isNotEmpty(vo.getGravel1Act())) {
            sum = sum.add(vo.getGravel1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel2Act())) {
            sum = sum.add(vo.getGravel2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel3Act())) {
            sum = sum.add(vo.getGravel3Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel4Act())) {
            sum = sum.add(vo.getGravel4Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel5Act())) {
            sum = sum.add(vo.getGravel5Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel6Act())) {
            sum = sum.add(vo.getGravel6Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder1Act())) {
            sum = sum.add(vo.getPowder1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder2Act())) {
            sum = sum.add(vo.getPowder2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder3Act())) {
            sum = sum.add(vo.getPowder3Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getAsphaltAct())) {
            sum = sum.add(vo.getAsphaltAct());
        }
        if (ObjectUtils.isNotEmpty(vo.getRecycledMaterialAct())) {
            sum = sum.add(vo.getRecycledMaterialAct());
        }
        return sum;
    }


    /**
     * 总量校核
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltProduceWeightVo> produceWeight(NAsphaltDataWeightBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.produceWeight(bo, pageQuery.build()));
    }

    /**
     * 生产数据监控重量列表
     * @param bo
     * @return
     */
    @Override
    public List<NAsphaltDataWeightVo> weightList(NAsphaltDataWeightBo bo) {
        List<NAsphaltDataWeightVo> list = baseMapper.weightList(bo);
        list = subList(list, bo.getId());
        List<NAsphaltDataWeightVo> data = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
            data = handleList(list, false, false);
        }
        return data;
    }

    /**
     * 生产数据监控重量列表-按天统计
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> dayWeightList(NAsphaltDataWeightBo bo) {
        // 列表
//        List<NAsphaltDataWeightVo> list = baseMapper.weightList(bo);
        List<NAsphaltDataWeightVo> list = baseMapper.weightListTotal(bo);
        list = subList(list, bo.getId());
        List<NAsphaltDataWeightVo> data = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
            data = daySummary(handleList(list, false, false), false);
        }
        // 合计
        NAsphaltDataWeightVo entity = calcDaySumTotal("合计", data);

        Map<String, Object> map = new HashMap<>();
        map.put("list", data);
        map.put("total", entity);

        return map;
    }


    /**
     * 获取级配占比
     * @param a
     * @param b
     * @return
     */
    public BigDecimal calcJp(BigDecimal a, BigDecimal b) {
        // 不为空且除数不为0
        if (ObjectUtils.isNotEmpty(a) && ObjectUtils.isNotEmpty(b) && b.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal data = a.divide(b, 4, BigDecimal.ROUND_HALF_UP);
            BigDecimal res = data.multiply(new BigDecimal(100));
            return res;
        } else {
            return null;
        }
    }

    /**
     * 昆仑拌合站生产总量获取从2024.7.30开始的数据 1702248790544715799
     * 德通拌合站生产总量获取从2024.8.23开始的数据 1702248790544715798
     * @param list
     * @return
     */
    public List<NAsphaltDataWeightVo> subList(List<NAsphaltDataWeightVo> list, String deptId) {
        List<NAsphaltDataWeightVo> subList = list;
        if (ObjectUtils.isNotEmpty(deptId) && deptId.equals("1702248790544715799")) {
            LocalDate startLocalDate = LocalDate.of(2024, 7, 30);
            Instant instant = startLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
            Date startDate = Date.from(instant);
            subList = list.stream().filter(item -> item.getDischargeTime().after(startDate)).collect(Collectors.toList());
        }
        if (ObjectUtils.isNotEmpty(deptId) && deptId.equals("1702248790544715798")) {
            LocalDate startLocalDate = LocalDate.of(2024, 8, 23);
            Instant instant = startLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
            Date startDate = Date.from(instant);
            subList = list.stream().filter(item -> item.getDischargeTime().after(startDate)).collect(Collectors.toList());
        }
        return subList;
    }

    /**
     * 按日统计数据
     * @param list
     * @return
     */
    public List<NAsphaltDataWeightVo> daySummary(List<NAsphaltDataWeightVo> list, Boolean dayFlag) {
//        list = handleList(list, false);
        List<NAsphaltDataWeightVo> resList = new ArrayList<>();
        Map<String, List<NAsphaltDataWeightVo>> collect = list.stream().collect(Collectors.groupingBy(o -> parse_yyyyMMdd(transDateToLocal(o.getDischargeTime()))));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        if (dayFlag) {
            collect.forEach((key, val) -> {
                if (date.equals(key)) {
                    NAsphaltDataWeightVo entity = calcDaySum(key, val);
                    resList.add(entity);
                }
            });
        } else {
            collect.forEach((key, val) -> {
                NAsphaltDataWeightVo entity = calcDaySum(key, val);
                resList.add(entity);
            });
        }
        // 时间倒序
//        resList.stream().sorted((t1, t2) -> new Date(t2.getCurrDate()).compareTo(new Date(t1.getCurrDate())));
        List<NAsphaltDataWeightVo> data = resList.stream().sorted((t1, t2) -> LocalDate.parse(t2.getCurrDate()).compareTo(LocalDate.parse(t1.getCurrDate()))).collect(Collectors.toList());
        return data;
    }

    /**
     * 日期格式转换
     * @param date
     * @return
     */
    public LocalDateTime transDateToLocal(Date date) {
        LocalDateTime time = null;
        if (ObjectUtils.isNotEmpty(date)) {
            time = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }
        return time;
    }

    public String parse_yyyyMMdd(LocalDateTime time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return df.format(time);
    }

    /**
     * 计算当前数据的骨料总重
     * @param vo
     * @return
     */
    public BigDecimal calcGlSum(NAsphaltDataWeightVo vo) {
        BigDecimal sum = BigDecimal.ZERO;
        if (ObjectUtils.isNotEmpty(vo.getGravel1Act())) {
            sum = sum.add(vo.getGravel1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel2Act())) {
            sum = sum.add(vo.getGravel2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel3Act())) {
            sum = sum.add(vo.getGravel3Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel4Act())) {
            sum = sum.add(vo.getGravel4Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel5Act())) {
            sum = sum.add(vo.getGravel5Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getGravel6Act())) {
            sum = sum.add(vo.getGravel6Act());
        }
        return sum;
    }

    /**
     * 计算当前数据的粉料总重
     * @param vo
     * @return
     */
    public BigDecimal calcFlSum(NAsphaltDataWeightVo vo) {
        BigDecimal sum = BigDecimal.ZERO;
        if (ObjectUtils.isNotEmpty(vo.getPowder1Act())) {
            sum = sum.add(vo.getPowder1Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder2Act())) {
            sum = sum.add(vo.getPowder2Act());
        }
        if (ObjectUtils.isNotEmpty(vo.getPowder3Act())) {
            sum = sum.add(vo.getPowder3Act());
        }
        return sum;
    }

    /**
     * 按天计算集合中的总重
     * @param list
     */
    public NAsphaltDataWeightVo calcDaySum(String date, List<NAsphaltDataWeightVo> list) {
        NAsphaltDataWeightVo entity = new NAsphaltDataWeightVo();
        if (ObjectUtils.isNotEmpty(list)) {
//            list = handleList(list, false);
            BigDecimal glSum = BigDecimal.ZERO;
            BigDecimal flSum = BigDecimal.ZERO;
            BigDecimal lqSum = BigDecimal.ZERO;
            BigDecimal zslSum = BigDecimal.ZERO;
            BigDecimal gl1Sum = BigDecimal.ZERO;
            BigDecimal gl2Sum = BigDecimal.ZERO;
            BigDecimal gl3Sum = BigDecimal.ZERO;
            BigDecimal gl4Sum = BigDecimal.ZERO;
            BigDecimal gl5Sum = BigDecimal.ZERO;
            BigDecimal gl6Sum = BigDecimal.ZERO;
            BigDecimal fl1Sum = BigDecimal.ZERO;
            BigDecimal fl2Sum = BigDecimal.ZERO;
            BigDecimal fl3Sum = BigDecimal.ZERO;
            BigDecimal ylSum = BigDecimal.ZERO;
            for (NAsphaltDataWeightVo vo : list) {
                glSum = glSum.add(calcGlSum(vo));
                flSum = flSum.add(calcFlSum(vo));
                lqSum = lqSum.add(ObjectUtils.isNotEmpty(vo.getAsphaltAct()) ? vo.getAsphaltAct() : BigDecimal.ZERO);
                zslSum = zslSum.add(ObjectUtils.isNotEmpty(vo.getRecycledMaterialAct()) ? vo.getRecycledMaterialAct() : BigDecimal.ZERO);
                gl1Sum = gl1Sum.add(ObjectUtils.isNotEmpty(vo.getGravel1Act()) ? vo.getGravel1Act() : BigDecimal.ZERO);
                gl2Sum = gl2Sum.add(ObjectUtils.isNotEmpty(vo.getGravel2Act()) ? vo.getGravel2Act() : BigDecimal.ZERO);
                gl3Sum = gl3Sum.add(ObjectUtils.isNotEmpty(vo.getGravel3Act()) ? vo.getGravel3Act() : BigDecimal.ZERO);
                gl4Sum = gl4Sum.add(ObjectUtils.isNotEmpty(vo.getGravel4Act()) ? vo.getGravel4Act() : BigDecimal.ZERO);
                gl5Sum = gl5Sum.add(ObjectUtils.isNotEmpty(vo.getGravel5Act()) ? vo.getGravel5Act() : BigDecimal.ZERO);
                gl6Sum = gl6Sum.add(ObjectUtils.isNotEmpty(vo.getGravel6Act()) ? vo.getGravel6Act() : BigDecimal.ZERO);
                fl1Sum = fl1Sum.add(ObjectUtils.isNotEmpty(vo.getPowder1Act()) ? vo.getPowder1Act() : BigDecimal.ZERO);
                fl2Sum = fl2Sum.add(ObjectUtils.isNotEmpty(vo.getPowder2Act()) ? vo.getPowder2Act() : BigDecimal.ZERO);
                fl3Sum = fl3Sum.add(ObjectUtils.isNotEmpty(vo.getPowder3Act()) ? vo.getPowder3Act() : BigDecimal.ZERO);
                if (ObjectUtils.isNotEmpty(glSum)) {
                    ylSum = ylSum.add(glSum);
                }
                if (ObjectUtils.isNotEmpty(flSum)) {
                    ylSum = ylSum.add(flSum);
                }
                if (ObjectUtils.isNotEmpty(lqSum)) {
                    ylSum = ylSum.add(lqSum);
                }
                if (ObjectUtils.isNotEmpty(zslSum)) {
                    ylSum = ylSum.add(zslSum);
                }
            }
            entity.setGl1SumWeight(gl1Sum);
            entity.setGl2SumWeight(gl2Sum);
            entity.setGl3SumWeight(gl3Sum);
            entity.setGl4SumWeight(gl4Sum);
            entity.setGl5SumWeight(gl5Sum);
            entity.setGl6SumWeight(gl6Sum);
            entity.setFl1SumWeight(fl1Sum);
            entity.setFl2SumWeight(fl2Sum);
            entity.setGlSumWeight(glSum);
            entity.setFlSumWeight(glSum);
            entity.setLqSumWeight(lqSum);
            entity.setZslSumWeight(zslSum);
            entity.setYlSumWeight(ylSum);
            entity.setCurrDate(date);
        }
        return entity;
    }

    /**
     * 按天计算集合中的总重合计
     * @param date
     * @param list
     * @return
     */
    public NAsphaltDataWeightVo calcDaySumTotal(String date, List<NAsphaltDataWeightVo> list) {
        NAsphaltDataWeightVo entity = new NAsphaltDataWeightVo();
        if (ObjectUtils.isNotEmpty(list)) {
            BigDecimal glSum = BigDecimal.ZERO;
            BigDecimal flSum = BigDecimal.ZERO;
            BigDecimal lqSum = BigDecimal.ZERO;
            BigDecimal zslSum = BigDecimal.ZERO;
            BigDecimal gl1Sum = BigDecimal.ZERO;
            BigDecimal gl2Sum = BigDecimal.ZERO;
            BigDecimal gl3Sum = BigDecimal.ZERO;
            BigDecimal gl4Sum = BigDecimal.ZERO;
            BigDecimal gl5Sum = BigDecimal.ZERO;
            BigDecimal gl6Sum = BigDecimal.ZERO;
            BigDecimal fl1Sum = BigDecimal.ZERO;
            BigDecimal fl2Sum = BigDecimal.ZERO;
            BigDecimal fl3Sum = BigDecimal.ZERO;
            BigDecimal ylSum = BigDecimal.ZERO;
            for (NAsphaltDataWeightVo vo : list) {
                glSum = glSum.add(ObjectUtils.isNotEmpty(vo.getGlSumWeight()) ? vo.getGlSumWeight() : BigDecimal.ZERO);
                flSum = flSum.add(ObjectUtils.isNotEmpty(vo.getFlSumWeight()) ? vo.getFlSumWeight() : BigDecimal.ZERO);
                lqSum = lqSum.add(ObjectUtils.isNotEmpty(vo.getLqSumWeight()) ? vo.getLqSumWeight() : BigDecimal.ZERO);
                zslSum = zslSum.add(ObjectUtils.isNotEmpty(vo.getZslSumWeight()) ? vo.getZslSumWeight() : BigDecimal.ZERO);
                ylSum = ylSum.add(ObjectUtils.isNotEmpty(vo.getYlSumWeight()) ? vo.getYlSumWeight() : BigDecimal.ZERO);
                gl1Sum = gl1Sum.add(ObjectUtils.isNotEmpty(vo.getGl1SumWeight()) ? vo.getGl1SumWeight() : BigDecimal.ZERO);
                gl2Sum = gl2Sum.add(ObjectUtils.isNotEmpty(vo.getGl2SumWeight()) ? vo.getGl2SumWeight() : BigDecimal.ZERO);
                gl3Sum = gl3Sum.add(ObjectUtils.isNotEmpty(vo.getGl3SumWeight()) ? vo.getGl3SumWeight() : BigDecimal.ZERO);
                gl4Sum = gl4Sum.add(ObjectUtils.isNotEmpty(vo.getGl4SumWeight()) ? vo.getGl4SumWeight() : BigDecimal.ZERO);
                gl5Sum = gl5Sum.add(ObjectUtils.isNotEmpty(vo.getGl5SumWeight()) ? vo.getGl5SumWeight() : BigDecimal.ZERO);
                gl6Sum = gl6Sum.add(ObjectUtils.isNotEmpty(vo.getGl6SumWeight()) ? vo.getGl6SumWeight() : BigDecimal.ZERO);
                fl1Sum = fl1Sum.add(ObjectUtils.isNotEmpty(vo.getFl1SumWeight()) ? vo.getFl1SumWeight() : BigDecimal.ZERO);
                fl2Sum = fl2Sum.add(ObjectUtils.isNotEmpty(vo.getFl2SumWeight()) ? vo.getFl2SumWeight() : BigDecimal.ZERO);
                fl3Sum = fl3Sum.add(ObjectUtils.isNotEmpty(vo.getFl3SumWeight()) ? vo.getFl3SumWeight() : BigDecimal.ZERO);
            }
            entity.setGl1SumWeight(gl1Sum);
            entity.setGl2SumWeight(gl2Sum);
            entity.setGl3SumWeight(gl3Sum);
            entity.setGl4SumWeight(gl4Sum);
            entity.setGl5SumWeight(gl5Sum);
            entity.setGl6SumWeight(gl6Sum);
            entity.setFl1SumWeight(fl1Sum);
            entity.setFl2SumWeight(fl2Sum);
            entity.setGlSumWeight(glSum);
            entity.setFlSumWeight(glSum);
            entity.setLqSumWeight(lqSum);
            entity.setZslSumWeight(zslSum);
            entity.setYlSumWeight(ylSum);
            entity.setCurrDate(date);
        }
        return entity;
    }


    /**
     * 首页数据汇总
     *
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> homeCount(NAsphaltDataWeightBo bo) {
        List<NAsphaltDataWeightVo> list = baseMapper.weightListTotal(bo);
//        list = daySummary(list, false);
        // 生产数据汇总
        BigDecimal dataSum = BigDecimal.ZERO;
        List<BigDecimal> gl1Y = new ArrayList<>();
        List<BigDecimal> gl2Y = new ArrayList<>();
        List<BigDecimal> gl3Y = new ArrayList<>();
        List<BigDecimal> gl4Y = new ArrayList<>();
        List<BigDecimal> gl5Y = new ArrayList<>();
        List<BigDecimal> gl6Y = new ArrayList<>();
        List<BigDecimal> fl1Y = new ArrayList<>();
        List<BigDecimal> fl2Y = new ArrayList<>();
        List<BigDecimal> fl3Y = new ArrayList<>();
        List<BigDecimal> lqY = new ArrayList<>();
        List<BigDecimal> zslY = new ArrayList<>();
        List<BigDecimal> tempY = new ArrayList<>();
        List<BigDecimal> ysbY = new ArrayList<>();
        List<String> timeX = new ArrayList<>();

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        // 生产总量 (昆仑拌合站的数据从7月30号开始的)
        for (NAsphaltDataWeightVo vo : list) {
//            if (ObjectUtils.isNotEmpty(vo.getYlSumWeight())) {
//                dataSum = dataSum.add(vo.getYlSumWeight());
//            }
            if (ObjectUtils.isNotEmpty(vo.getDischargeTime())) {
                String format = sdf1.format(vo.getDischargeTime());
                timeX.add(format);
            } else {
                timeX.add(null);
            }
//            timeX.add(vo.getDischargeTime().toString());
            gl1Y.add(calcJp(vo.getGravel1Act(), vo.getBatchWeight()));
            gl2Y.add(calcJp(vo.getGravel2Act(), vo.getBatchWeight()));
            gl3Y.add(calcJp(vo.getGravel3Act(), vo.getBatchWeight()));
            gl4Y.add(calcJp(vo.getGravel4Act(), vo.getBatchWeight()));
            gl5Y.add(calcJp(vo.getGravel5Act(), vo.getBatchWeight()));
            gl6Y.add(calcJp(vo.getGravel6Act(), vo.getBatchWeight()));
            fl1Y.add(calcJp(vo.getPowder1Act(), vo.getBatchWeight()));
            fl2Y.add(calcJp(vo.getPowder2Act(), vo.getBatchWeight()));
            fl3Y.add(calcJp(vo.getPowder3Act(), vo.getBatchWeight()));
            lqY.add(calcJp(vo.getAsphaltAct(), vo.getBatchWeight()));
            zslY.add(calcJp(vo.getRecycledMaterialAct(), vo.getBatchWeight()));
            tempY.add(vo.getAsphaltTemperature());
            ysbY.add(vo.getAsphaltAggregateRatio());
        }
        // 生产批次总量
        int dataTotal = ObjectUtils.isNotEmpty(list) ? list.size() : 0;
        // 预警总数
        int ysTotal = 0;
        // 当日生产总量
        BigDecimal subDataSum = BigDecimal.ZERO;
        // 生产总量
        for (NAsphaltDataWeightVo vo : daySummary(list, false)) {
            if (ObjectUtils.isNotEmpty(vo.getYlSumWeight())) {
                dataSum = dataSum.add(vo.getYlSumWeight());
            }
        }
        // 当日生产总量
        for (NAsphaltDataWeightVo vo : daySummary(list, true)) {
            if (ObjectUtils.isNotEmpty(vo.getYlSumWeight())) {
                subDataSum = subDataSum.add(vo.getYlSumWeight());
            }
        }

        // 最后生产时间
        String dataTime = "";
        if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (ObjectUtils.isNotEmpty(list.get(0).getDischargeTime())) {
                dataTime = sdf2.format(list.get(0).getDischargeTime());
            }
//            dataTime = list.get(0).getDischargeTime().toString();
        }

        // 当日生产总量
//        Map<String, Object> subMap = dayWeightList(bo);
//        if (ObjectUtils.isNotEmpty(subMap.get("total"))) {
//            NAsphaltDataWeightVo total = (NAsphaltDataWeightVo) subMap.get("total");
//            if (ObjectUtils.isNotEmpty(total.getYlSumWeight())) {
//                subDataSum = total.getYlSumWeight().divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
//            }
//        }
        // 生产总量 当日生产总量 千克转为吨
        dataSum = dataSum.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
        subDataSum = subDataSum.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);

        Map<String, Object> count = new HashMap<>();
        count.put("sczl", dataSum);
        count.put("scpczl", dataTotal);
        count.put("yszs", ysTotal);
        count.put("zhscsj", dataTime);
        count.put("daySczl", subDataSum);

        // 级配曲线 - 没数据 暂时为空
        List<String> jpNames = new ArrayList<>();
        List<Double> jpY = new ArrayList<>();
        List<Double> jpX = new ArrayList<>();

        jpX.add(0.075d);
        jpX.add(0.15d);
        jpX.add(0.3d);
        jpX.add(0.6d);
        jpX.add(1.18d);
        jpX.add(2.36d);
        jpX.add(4.75d);
        jpX.add(9.5d);
        jpX.add(13.2d);
        jpX.add(16d);
        jpX.add(19d);
        jpX.add(26.5d);
        jpX.add(31.5d);

        for (int i=0; i<dataTotal; i++) {
            jpY.add(0d);
        }

        Map<String, Object> jpqx = new HashMap<>();
        jpqx.put("name", jpNames);
        jpqx.put("x", jpX);
        jpqx.put("y", jpY);

        // 内掺比
        List<String> ncbNames = new ArrayList<>();
        ncbNames.add("骨料1");
        ncbNames.add("骨料2");
        ncbNames.add("骨料3");
        ncbNames.add("骨料4");
        ncbNames.add("骨料5");
        ncbNames.add("骨料6");
        ncbNames.add("粉料1");
        ncbNames.add("粉料2");
        ncbNames.add("粉料3");
        ncbNames.add("沥青");
        ncbNames.add("再生料");

        Map<String, Object> ncb = new HashMap<>();
        ncb.put("name", ncbNames);
        ncb.put("x", timeX);
        ncb.put("y1", gl1Y);
        ncb.put("y2", gl2Y);
        ncb.put("y3", gl3Y);
        ncb.put("y4", gl4Y);
        ncb.put("y5", gl5Y);
        ncb.put("y6", gl6Y);
        ncb.put("y7", fl1Y);
        ncb.put("y8", fl2Y);
        ncb.put("y9", fl3Y);
        ncb.put("y10", lqY);
        ncb.put("y11", zslY);

        // 温度曲线
        List<String> wdNames = new ArrayList<>();
        wdNames.add("温度");

        Map<String, Object> wd = new HashMap<>();
        wd.put("name", wdNames);
        wd.put("x", timeX);
        wd.put("y", tempY);

        // 油石比
        List<String> ysbNames = new ArrayList<>();
        ysbNames.add("油石比");

        Map<String, Object> ysb = new HashMap<>();
        ysb.put("name", ysbNames);
        ysb.put("x", timeX);
        ysb.put("y", ysbY);

        Map<String, Object> res = new HashMap<>();
        res.put("count", count);
        res.put("jpqx", jpqx);
        res.put("ncb", ncb);
        res.put("wd", wd);
        res.put("ysb", ysb);

        // 按日统计出列表
//        Map<String, Object> map = dayWeightList(bo);
//        res.put("dayMap", map);

        return res;
    }
}
